package com.controller.zk;

import com.alibaba.fastjson2.JSONObject;
import com.bean.zk.Emp;
import com.dao.zk.HousehodeDAO;
import com.service.zk.EmpService;
import com.util.zk.OCR_Ali;
import com.util.zk.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
public class EmpController {

    @Resource
    private EmpService empService;
    @Resource
    private HousehodeDAO househodeDAO;


    @RequestMapping("/dologin")
    @ResponseBody
    public Result logincheck(String account, String pswd, String usertype, HttpServletRequest req) {
        boolean result = empService.getIdByNameAndPswd(account, pswd, usertype, req);
        Result rs = new Result();
        rs.setRs(result);
        return rs;
    }

    @RequestMapping("/page/getemps")
    @ResponseBody
    public List<Map<String, Object>> get() {
        return empService.getEmps();
    }

    @RequestMapping("/page/selemp")
    @ResponseBody
    public List<Map<String, Object>> getByBlur(String ename, String did) {
        return empService.getEmpByBlur(ename, did);
    }

    @RequestMapping("/page/addemp")
    @ResponseBody
    public boolean add(Emp emp) {
        Map<String, Object> maps = empService.selectByPhone(emp);
        if (maps!=null) {
            return false;
        } else {
            empService.insertemp(emp);
            return true;
        }
    }

    /**
     * 根据eid 删除
     *
     * @param eid
     * @return
     */
    @RequestMapping("/page/delemp/{eid}")
    @ResponseBody
    public int delete(@PathVariable String eid) {
        return empService.delete(eid);
    }

    /**
     * 根据eid 查找
     *
     * @param eid
     * @return
     */
    @RequestMapping("/page/selemp/{eid}")
    @ResponseBody
    public Map<String, Object> select(@PathVariable String eid) {
        return empService.select(eid);
    }

    /**
     * 更新用户
     *
     * @param eid
     * @param emp
     * @return
     */
    @RequestMapping("/page/updatemp/{eid}")
    @ResponseBody
    public boolean update(@PathVariable String eid, Emp emp) {
        if (empService.update(eid, emp) == 1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 上传图片
     *
     * @param sc
     * @return
     */
    @RequestMapping(value = "/page/uploadimg", method = RequestMethod.POST)
    @ResponseBody
    public Object uploadimg(MultipartFile sc) {
        JSONObject jsonObject = null;
        try {
            jsonObject = OCR_Ali.orcService(sc.getBytes());
            System.out.println(jsonObject.toJSONString());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return jsonObject;
    }

    /**
     * 退出登录
     *
     * @param request 请求
     * @return
     */
    @RequestMapping(value = "/quitlogin")
    @ResponseBody
    public boolean quit(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("user",null);
        return true;
    }

    /**
     * 电话号码登录
     *
     * @param phonenum 电话号码
     * @return
     */
    @RequestMapping(value = "/page/phonelogin")
    @ResponseBody
    public int dophoneLogin(String phonenum, String usertype, HttpServletRequest request) {
        if (usertype.equals("emp")) {
            Emp emp = new Emp();
            emp.setPhone(phonenum);
            Map<String, Object> maps = empService.selectByPhone(emp);
            if (maps == null) {
                //            查询不到用户，用户不存在
                return 0;
            } else {
                request.getSession().setAttribute("user", maps);
                return 1;
            }
        } else {
            Map<String,Object>  hs = househodeDAO.selectByPhone(phonenum);

            if (hs.size() == 0) {
//            查询不到用户，用户不存在
                return 0;
            } else {
                request.getSession().setAttribute("user", hs);
                return 1;
            }
        }

    }


}
